package vn.com.vega.music.utils;

import java.lang.ref.WeakReference;

import android.os.AsyncTask;
import vn.com.vega.chacha.v2.ActivityBase;
import vn.com.vega.music.clientserver.JsonAlbum;
import vn.com.vega.music.objects.Album;

public class AlbumUtil {
	public interface LikeAndUnLikeAlbumListener {
		public void startDoLike(Album album);
		public void likeSuccess(Album album);
		public void likeFail(Album album, String errorMsg);
		
		public void startDoUnlike(Album album);
		public void unlikeSuccess(Album album);
		public void unlikeFail(Album album, String errorMsg);
	}
	
	static class LikeAndUnlikeAlbum extends AsyncTask<Void, Void, Void> {
		WeakReference<ActivityBase> activityBaseRfc;
		Album album;
		LikeAndUnLikeAlbumListener likeAndUnlikeAlbumListener;
		boolean doLike;
		String errorMessage;
		
		public LikeAndUnlikeAlbum(ActivityBase act, Album album, LikeAndUnLikeAlbumListener listener, boolean doLile) {
			activityBaseRfc = new WeakReference<ActivityBase>(act);
			this.album = album;
			likeAndUnlikeAlbumListener = listener;
			this.doLike = doLile;
		}
		
		private boolean checkIsLive() {
			ActivityBase act = activityBaseRfc.get();
			if (act == null || act.isDestroy) {
				return false;
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return null;
			}
			if (album == null) {
				errorMessage = "";
				return null;
			}
			JsonAlbum jsonAlbum = JsonAlbum.likeAlbum(album.id);
			if (jsonAlbum.isSuccess()) {
				errorMessage = null;
				return null;
			} else {
				errorMessage = jsonAlbum.getErrorMessage();
				if (errorMessage == null) {
					errorMessage = "";
				}
			}
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (!checkIsLive() || isCancelled()) {
				return;
			}
			if (likeAndUnlikeAlbumListener != null) {
				if (doLike) {
					if (errorMessage == null) {
						likeAndUnlikeAlbumListener.likeSuccess(album);
					} else {
						likeAndUnlikeAlbumListener.likeFail(album, errorMessage);
					}
				} else {
					if (errorMessage == null) {
						likeAndUnlikeAlbumListener.unlikeSuccess(album);
					} else {
						likeAndUnlikeAlbumListener.unlikeFail(album, errorMessage);
					} 
				}
			}
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return;
			}
			if (likeAndUnlikeAlbumListener != null) {
				if (doLike) {
					likeAndUnlikeAlbumListener.startDoLike(album);
				} else {
					likeAndUnlikeAlbumListener.startDoUnlike(album);
				}
			}
		}
	}
	
	public static void likeAlbum(ActivityBase act, Album alum, LikeAndUnLikeAlbumListener likeAlbumListener) {
		LikeAndUnlikeAlbum likeAndUnlikeAlbum = new LikeAndUnlikeAlbum(act, alum, likeAlbumListener, true);
		likeAndUnlikeAlbum.execute();
	}
	
	public static void unLikeAlbum(ActivityBase act, Album alum, LikeAndUnLikeAlbumListener likeAlbumListener) {
		LikeAndUnlikeAlbum likeAndUnlikeAlbum = new LikeAndUnlikeAlbum(act, alum, likeAlbumListener, false);
		likeAndUnlikeAlbum.execute();
	}
}
